


import 'package:flutter/cupertino.dart';
import 'package:oral_cloud/config/constant.dart';
import 'package:oral_cloud/config/storage_manager.dart';
import 'package:oral_cloud/model/user.dart';
import 'package:shared_preferences/shared_preferences.dart';

class UserModel extends ChangeNotifier{
  static const String kUser='kUser';
  User _user;

  User get user => _user;

  UserModel(){
    var userMap=StorageManager.localStorage.getItem(kUser);
    _user=userMap!=null? User.fromJsonMap(userMap):null;
  }

  saveUser(User user){
    _updateLoginState(LoginState.logined);
    _user=user;
    notifyListeners();
    StorageManager.localStorage.setItem(kUser, user);
  }

  clearUser(){
    _user=null;
    _updateLoginState(LoginState.unlogin);
    notifyListeners();
    StorageManager.localStorage.deleteItem(kUser);
  }



  void _updateLoginState(int state) async{
    final prefs=await SharedPreferences.getInstance();
    prefs.setInt(Constant.kLoginStatue, state);
  }
}